package com.cty.json;

import com.cty.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("json")
@ResponseBody//作用：不通过视图解析器直接返回json数据
public class JsonController {

    @RequestMapping("data")
    //返回json数据直接返回一个对象就行
    public User data() {
        User user = new User();
        user.setAge(20);
        user.setName("cty");
        return user;
    }

    //返回集合怎么办，json中有数组这一数据
    @RequestMapping("data1")
    public List<User> data1() {
        List<User> users = new ArrayList<>();
        User user = new User();
        user.setAge(20);
        user.setName("cty");
        users.add(user);
        return users;
    }
}
